package com.example.clublesson01.fragments.playcart

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.kf.lib_base.base.BaseViewModel
import com.bw.kf.lib_base.entity.PlayCartInfoEntity
import com.bw.kf.lib_base.entity.PlayCartTopNavigationInfoEntity
import com.bw.kf.lib_base.entity.Res
import com.bw.kf.lib_base.entity.SearchTitleEntity
import com.bw.kf.lib_base.entity.TieCommentEntity
import com.bw.kf.lib_base.entity.TieDeatilEntity

import kotlinx.coroutines.launch

class PlayCartViewModel : BaseViewModel() {
  // 定义LiveData对象用于成功获取播放购物车顶部导航信息时的数据传递
val playCartTopNavigationInfoSuccess =
    MutableLiveData<Res<MutableList<PlayCartTopNavigationInfoEntity>>>()
// 定义LiveData对象用于失败获取播放购物车顶部导航信息时的错误信息传递
val playCartTopNavigationInfoFail = MutableLiveData<String>()
// 定义SingleLiveEvent对象用于成功获取播放购物车信息列表时的数据传递
val playCartInfoSuccess = MutableLiveData<Res<PlayCartInfoEntity>>()
// 定义SingleLiveEvent对象用于失败获取播放购物车信息列表时的错误信息传递
val playCartInfoFail = MutableLiveData<String>()

/**
 * 获取播放购物车顶部导航信息
 * 此函数通过网络请求获取播放购物车顶部导航信息，并通过LiveData对象传递结果
 */
fun getPlayCartTopNavigationInfo() {
    viewModelScope.launch {
        try {
            // 成功获取数据时，更新LiveData对象的值
            playCartTopNavigationInfoSuccess.value =
                apiService.getPlayCartTopNavigationInfo()
        } catch (e: Exception) {
            // 发生异常时，更新错误信息LiveData对象的值
            playCartTopNavigationInfoFail.value = e.message
        }
    }
}

/**
 * 获取播放购物车信息列表
 * 此函数通过网络请求获取播放购物车信息列表，并通过SingleLiveEvent对象传递结果
 *
 * @param isFocus 关注状态，用于筛选结果
 * @param pageNum 页码，用于分页查询
 * @param pageSize 每页大小，用于分页查询
 * @param sortType 排序类型，默认为"hot"
 */
fun getPalyCartInfoList(
    isFocus: String,
    pageNum: Int,
    pageSize: Int,
    sortType: String = "hot"
) {
    viewModelScope.launch {
        try {
            // 成功获取数据时，更新SingleLiveEvent对象的值
            playCartInfoSuccess.value =
                apiService.getPlayCartInfo(isFocus, pageNum, pageSize, sortType)
        } catch (e: Exception) {
            // 发生异常时，更新错误信息SingleLiveEvent对象的值，并记录日志
            playCartInfoFail.value = e.message
            Log.d("getPalyCartInfoList", e.message.toString())
        }
    }
}
    fun getPalyCartInfoListsearch(
        isFocus: String,
        keyword:String,
        pageNum: Int,
        pageSize: Int,
        sortType: String = "hot"
    ) {
        viewModelScope.launch {
            try {
                // 成功获取数据时，更新SingleLiveEvent对象的值
                playCartInfoSuccess.value =
                    apiService.getPlayCartInfosearch(isFocus,keyword, pageNum, pageSize, sortType)
            } catch (e: Exception) {
                // 发生异常时，更新错误信息SingleLiveEvent对象的值，并记录日志
                playCartInfoFail.value = e.message
                Log.d("getPalyCartInfoList", e.message.toString())
            }
        }
    }

    val tieDetailsuccess = MutableLiveData<TieDeatilEntity>()
    val tieDetailfail = MutableLiveData<String>()

    fun getPostDetail(postsId:String){
        viewModelScope.launch {
            try {
                tieDetailsuccess.value = apiService.getPostDetail(postsId)
            }catch (e:Exception){
                tieDetailfail.value = e.message
            }
        }
    }

    val tieCommentsuccess = MutableLiveData<TieCommentEntity>()
    val tieCommentfail = MutableLiveData<String>()

    fun getTieComment(pageNum:Int,pageSize:Int,postsId:String){
        viewModelScope.launch {
            try {
                tieCommentsuccess.value = apiService.getTieComment(pageNum, pageSize, postsId)
            }catch (e:Exception){
                tieCommentfail.value = e.message
            }
        }
    }
    val searchtitlesuccess = MutableLiveData<SearchTitleEntity>()
    val searchtitlefail = MutableLiveData<String>()
    fun getSearchTitle(){
        viewModelScope.launch {
            try {
                searchtitlesuccess.value = apiService.getSearchTitle()
            }catch (e:Exception){
                searchtitlefail.value = e.message
            }
        }
    }
}